<template>
  <div class="home">
    <div class="content">
      <div class="header">
        <el-page-header content="编辑页面" @back="goBack"></el-page-header>
      </div>
      <div class="selectMoudle">
        <div>
          <span class="text">模块</span>
          <el-radio-group v-model="editorData.radio1" fill="#1d2f70">
            <el-radio-button
              v-for="(item, index) in radio"
              :label="item.label"
              :key="index"
            ></el-radio-button>
          </el-radio-group>
        </div>
        <div>
          <el-button class="sendBtn" @click="send">
            发布
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </div>
      </div>
      <div class="Imagetitele">
        <div class="textSize">添加题图</div>
      </div>
      <div class="titleImage">
        <div class="algin">
          <el-upload
            accept="image/png, image/jpg, image/jpeg"
            class="avatar-uploader"
            :action="api.imgManage"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="editorData.titleImageUrl"
              :src="editorData.titleImageUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>
      <div class="inputBox">
        <input
          type="text"
          placeholder="输入标题"
          v-model="editorData.title"
          @change="onChangeEdit"
        />
      </div>
      <div class="editor">
        <textEditor
          @EditorChange="editorChange"
          :oldcontent="editorData.html"
          :oldremoveIds="editorData.deleImgId"
        ></textEditor>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from "axios";
import textEditor from "@/components/quillEditor";
// import { textReplace } from "@/common/textReplace";
import { post_draft } from "@/api/draft";
import { rich_text_img, rich_text_img_prefix } from "@/utils/global";
export default {
  name: "Editor",
  data() {
    return {
      editorData: {
        radio1: "创新建议",
        title: "",
        titleImageUrl: "",
        html: "",
        deleImgId: [],
        textReplace: "",
        deletTitleImageUrl: [],
        userId: this.$store.getters.userInfo._id,
      },
      headers: { Authorization: "Bearer " + this.$store.getters.token },
      radio: [
        {
          name: "创新建议",
          label: "创新建议",
        },
      ],
      api: {
        imgManage: rich_text_img,
        rich_text_img_prefix,
      },
      // timer: null
    };
  },
  components: {
    textEditor,
  },
  created() {
    // localStorage.removeItem("editorData");
    // let getLocalData = localStorage.getItem("editorData");
    // if (getLocalData) {
    //   this.editorData = JSON.parse(getLocalData);
    //   console.log("getLocalData", JSON.parse(getLocalData));
    // }
    // clearInterval(this.timer);
    // this.timer = null;
    // this.setTimer();
  },
  // destroyed: function() {
  //   // 每次离开当前界面时，清除定时器
  //   clearInterval(this.timer);
  //   this.timer = null;
  // },
  methods: {
    // setTimer() {
    //   let time = 1000 * 60 * 5;
    //   if (this.timer == null) {
    //     this.timer = setInterval(() => {
    //       console.log("开始定时...每过一秒执行一次");
    //       localStorage.setItem("editorData", JSON.stringify(this.editorData));
    //       this.$message({
    //         message: "自动保存成功",
    //         type: "success"
    //       });
    //     }, time);
    //   }
    // },
    goBack() {
      this.$router.go(-1);
    },
    handleAvatarSuccess(res, file) {
      console.log("handleAvatarSuccess", file, this.editorData);

      // this.editorData.titleImageUrl = URL.createObjectURL(file.raw);
      if (this.editorData.titleImageUrl) {
        let name = this.editorData.titleImageUrl.replace(
          this.api.rich_text_img_prefix,
          ""
        );
        this.editorData.deletTitleImageUrl.push(name);
      }
      this.editorData.titleImageUrl = file.response.url;
      // console.log("handleAvatarSuccess2222", this.editorData);
      this.onChangeEdit();
    },
    uploadSectionFile: function (param) {
      //自定义文件上传
      console.log("param", param);
      // var fileObj = param.file;
      var reader = new FileReader(); //调用FileReader
      reader.readAsDataURL(param.file); //将文件读取为 DataURL(base64)
      var that = this;
      reader.onload = function (evt) {
        //读取操作完成时触发。
        that.editorData.titleImageUrl = evt.target.result;
      };
    },
    editorChange(data) {
      this.editorData.html = data.html;
      this.editorData.deleImgId = data.removeIds;
      // this.editorData.textReplace = textReplace(data.html);
      console.log(this.editorData);
      this.onChangeEdit();
    },
    async onChangeEdit() {
      let editView = await post_draft(this.editorData);
      console.log("editView", editView);
      let edit_id = editView.data._id;
      this.$router.replace({
        path: "/editProposal",
        query: {
          _id: edit_id,
        },
      });
    },
    send() {
      if (this.editorData.titleImageUrl == "") {
        return this.$alert("请添加题图", "提示", {
          confirmButtonText: "确定",
          callback: () => {},
        });
      }
      if (this.editorData.title == "") {
        return this.$alert("请完善文章标题", "提示", {
          confirmButtonText: "确定",
          callback: () => {},
        });
      }
      if (this.editorData.html == "") {
        return this.$alert("请完善文章内容", "提示", {
          confirmButtonText: "确定",
          callback: () => {},
        });
      }
      // let deleArr = this.editorData.deleImgId.concat(
      //   this.editorData.deletTitleImageUrl
      // );
      // this.editorData.textReplace = textReplace(this.editorData.html);
      // console.log(deleArr);

      // if (deleArr.length == 0) return;

      // axios
      //   .post("https://jifen.zhportinfo.com/api/v1/delectImg", {
      //     deleImgArr: deleArr
      //   })
      //   .then(function(response) {
      //     console.log(response);
      //   })
      //   .catch(function(error) {
      //     console.log(error);
      //   });
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  background: #fff;
}
.content {
  width: 100%;
  .header {
    margin: 0 auto;
    width: 1200px;
    margin-top: 20px;
  }
  .selectMoudle {
    margin: 0 auto;
    width: 1200px;
    margin-top: 40px;
    display: flex;
    justify-content: space-between;
    .text {
      font-size: 20px;
      font-weight: 700;
      color: #757575;
      margin-right: 20px;
    }
    .sendBtn {
      background: #1d2f70;
      border: #1d2f70;
      color: #fff;
      &:hover {
        background: rgb(53, 89, 180);
      }
    }
  }
  .Imagetitele {
    width: 1200px;
    margin: 0 auto;
    margin-top: 40px;
    .textSize {
      font-size: 27px;
      font-weight: 700;
      color: #757575;
    }
  }
  .titleImage {
    width: 1200px;
    margin: 0 auto;
    margin-top: 20px;
    .algin {
      width: 800px;
      margin: 0 auto;
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 800px;
        height: 300px;
        line-height: 300px;
        text-align: center;
      }
      .avatar {
        width: 800px;
        display: block;
      }
    }
  }
  .inputBox {
    margin: 0 auto;
    width: 1200px;
    margin-top: 20px;
    input {
      outline-style: none;
      outline-style: none;
      border: 0px;
      border-bottom: 2px solid #ccc;
      width: 1200px;
      padding: 13px 14px;
      font-size: 28px;
      font-weight: 700;
      font-family: "Microsoft soft";
      box-sizing: border-box;
    }
  }
  // margin-top: -19px;
  padding: 20px;
  box-sizing: border-box;
  .editor {
    min-height: 500px;
    margin-top: 40px;
  }
}
</style>
